package com.hardware.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hardware.common.R;
import com.hardware.dto.OrdersDto;
import com.hardware.dto.PurchaseDto;
import com.hardware.dto.ShopDto;
import com.hardware.entity.*;
import com.hardware.service.EmployeeService;
import com.hardware.service.PurchaseService;
import com.hardware.service.ShopService;
import com.hardware.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

import static com.hardware.utils.RedisConstants.CACHE_ORDER_KEY;

@Slf4j
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private EmployeeService employeeService;


    @GetMapping("/page")
    @Cacheable(value = "cache:purchase:",key = "#page +'_'+ #pageSize +'_'+ #number +'_'+ #beginTime +'_'+ #endTime")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime) throws ParseException {
        Page<Purchase> pageInfo = new Page<>(page, pageSize);
        Page<PurchaseDto> purchaseDtoPage = new Page<>();

        purchaseService.page(pageInfo);

        BeanUtils.copyProperties(pageInfo,purchaseDtoPage,"records");

        List<Purchase> records = pageInfo.getRecords();

        List<PurchaseDto> list = records.stream().map((item) -> {
            PurchaseDto purchaseDto = new PurchaseDto();

            BeanUtils.copyProperties(item,purchaseDto);
            //供应人和公司
            Long supplierId = item.getSupplierId();
            Supplier supplier = supplierService.getById(supplierId);
            purchaseDto.setSupplierCompany(supplier.getCompany());
            purchaseDto.setSupplierName(supplier.getName());
            //商品名称和图片
            Long shopId = item.getShopId();
            Shop shop = shopService.getById(shopId);
            purchaseDto.setImage(shop.getImage());
            purchaseDto.setShopName(shop.getName());

            Employee employee = employeeService.getById(item.getEmployeeId());
            purchaseDto.setEmployeeName(employee.getName());

            return purchaseDto;
        }).collect(Collectors.toList());
        purchaseDtoPage.setRecords(list);
        return R.success(purchaseDtoPage);
    }

    /**
     * 更新状态
     * @param purchase
     * @return
     */
    @PutMapping
    @CacheEvict(value = "cache:purchase:",allEntries = true)
    public R<String> status(@RequestBody Purchase purchase){
        //已支付，加库存量
        if(purchase.getStatus() == 2){
            ShopDto shopDto = new ShopDto();
            shopDto.setId(purchase.getShopId());
            shopDto.setStock(purchase.getNumber());
            shopDto.setStatus(purchase.getNumber());
            shopService.addStock(shopDto);
        }
        purchaseService.updateById(purchase);
        return R.success("");
    }
}
